using System;
using System.Diagnostics;
using System.Collections;

namespace CUtil
{
	/// <summary>
	/// 
	/// </summary>
	public class NameObjectCollection: 	System.Collections.Specialized.NameObjectCollectionBase 
	{
		//		private string[] keys;
		//		private object[] values;


		/// <summary>
		/// 
		/// </summary>
		public object this [string Name]   // indexer declaration
		{
			get 
			{
				//return this.textItems[index];
				return this.BaseGet(Name);
			}
			set 
			{
				this.BaseSet(Name,value);
			}
		}
	

		/// <summary>
		/// 
		/// </summary>
		public object this [int index]   // indexer declaration
		{
			get 
			{
				//return this.textItems[index];
				return this.BaseGet(index);
			}
			set 
			{
				this.BaseSet(index,value);
			}
		}
	
		/// <summary>
		/// 
		/// </summary>
		/// <param name="Name"></param>
		/// <param name="val"></param>
		public void Add(string Name,object val)
		{
			this.BaseAdd(Name,val);
			
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="Name"></param>
		/// <param name="val"></param>
		public void Add(object Name,object val)
		{
			this.BaseAdd(Name.ToString(),val);
			
		}
		/// <summary>
		/// 
		/// </summary>
		public void Clear()
		{
			this.BaseClear();
			
			
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="Name"></param>
		/// <returns></returns>
		public bool Contains(string Name)
		{
			foreach(string str in this.BaseGetAllKeys())
			{
				if(str == Name)
					return true;
			}
			return false;
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="Name"></param>
		public void Remove(string Name)
		{
			this.BaseRemove(Name);
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="index"></param>
		public void RemoveAt(int index)
		{
			this.BaseRemoveAt(index);
		}
        //public bool SortByKey()
        //{
        //    return SortByValue();
        //}
        public bool SortByKey_WhichIsFloat()
        {
            try
            {
                int dimCollection = this.Keys.Count;
                double[] keys = new double[this.Keys.Count];
                object[] objs= new object[this.Keys.Count];
                


                for (int i = 0; i < dimCollection; i++)
                {
                    //a.Add(Convert.ToDouble(this.Keys[i]), this[i]);
                    keys [i] = Convert.ToDouble(this.Keys[i]);//this.Keys[i];
                    objs[i] = this[i]; 

                }


                int j, k;
                //int j1, bis1, k1;
                double hVal, h1Val;
                object hKey, h1Key;

                //bis = val .Count;
                k = this.Keys.Count / 2;
                double f = (double)dimCollection / (double)2;

                if (f - k < 0)
                    k -= 1;


                while (k > 0)
                {
                    for (int i = 0; i < dimCollection - k; i++)
                    {
                        j = i;
                        while (j >= 0 && ((double)keys[j] > (double)keys[j + k]))
                        {
                            hVal = keys [j];
                            hKey = objs[j];

                            keys [j] = keys [j + k];
                            objs[j] = objs[j + k];

                            keys [j + k] = hVal;
                            objs[j + k] = hKey;
                            h1Val = Convert.ToDouble(this.Keys[j]);

                            h1Key = this[j];

                            this[j] = this[j + k];

                            this[j + k] = h1Key;
                            //this.Keys[j + k] = h1Key;
                            if (j > k)
                            {
                                j = j - k;
                            }
                            else
                            {
                                j = 0;
                            }
                        }

                    }
                    k = k / 2;

                }
                this.Clear();
                for (int i = 0; i < dimCollection; i++)
                {
                    this.Add( keys [i], objs[i]);
                }


                return true;
            }
            catch (Exception exc)
            {
                System.Diagnostics.Debug.Assert(false, "error sorting collection ", exc.Message);
                return false;
            }
        }
		public bool SortByValue()
		{
			try
			{
                int dimCollection = this.Keys.Count;
                string[] keys = new string[this.Keys.Count];
                double[] a = new double[this.Keys.Count];


                for (int i = 0; i < dimCollection; i++)
				{
                    //a.Add(Convert.ToDouble(this.Keys[i]), this[i]);
                    a[i] = Convert.ToDouble(this[i]);
                    keys[i] = this.Keys[i];

				}


				int j,k;
				//int j1, bis1, k1;
				double hVal,h1Val;
                string hKey, h1Key;

				//bis = a.Count;
                k = this.Keys.Count / 2;
                double f = (double)dimCollection / (double)2;
				
				if(f - k < 0)
					k -= 1;
				
				
				while(k > 0)
				{
                    for (int i = 0; i < dimCollection - k; i++)
					{
						j = i;
						while(j >= 0 && ((double)a[j] > (double)a[j+ k ]) )
						{
							hVal = a[j];
                            hKey = keys[j];

							a[j] = a[j + k ];
                            keys[j] = keys[j + k];
                            
							a[j + k ] = hVal;
                            keys[j + k] = hKey;
                            h1Val = Convert.ToDouble(this[j]);

                            h1Key = this.Keys[j];

							this[j] = this[j + k ];

							this[j + k ] = h1Val;
                            //this.Keys[j + k] = h1Key;
							if (j > k )
							{
								j = j - k;
							}
							else
							{
								j = 0;
							}
						}

					}
					k = k / 2;

				}
                this.Clear();
                for (int i = 0; i < dimCollection; i++)
                {
                    this.Add(keys[i], a[i]);
                }


				return true;
			}
			catch(Exception exc)
			{
				System.Diagnostics.Debug.Assert(false,"error sorting collection ", exc.Message);
				return false;
			}
		}

        public bool SortByValueOld()
        {
            try
            {
                SortedList sortedListOfNames = new SortedList();
                //System.Collections.Specialized.NameValueCollection nvc = new System.Collections.Specialized.NameValueCollection();
                //nvc.So
                for (int i = 0; i < this.Count; i++)
                {
                    object c = this[i];
                    if (!sortedListOfNames.ContainsValue(c))
                        sortedListOfNames.Add(Convert.ToString(c, CUtil.Conversions.CultureAmerican), c);
                }
                NameObjectCollection arrListResult = new NameObjectCollection();
                IList sortedList = sortedListOfNames.GetValueList();
                //IList sortedList = sortedListOfNames.get();
                for (int i = 0; i < sortedList.Count; i++)
                {
                    //arrListResult.Add(this[j]);
                    double fVal = Convert.ToDouble(sortedList[i]);
                    for (int j = 0; j < this.Count; j++)
                    {
                        if (fVal == Convert.ToDouble( this[j]))
                            arrListResult.Add(this.Keys[j], this[j]);
                    }

                }
                this.Clear();
                for (int i = 0; i < arrListResult.Count; i++)
                {
                    this.Add(arrListResult.Keys[i], arrListResult[i]);
                }

                return true;

            }
            catch (Exception exc)
            {
                Debug.Assert(false, "Error sorting array list", exc.Message);

            }
            return false;


        }
	
		
	}
}
